package com.erp.module.crm

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import com.erp.CaptureActivity
import com.erp.module.crm.xXCrm.ClientKActivity
import com.erp.module.crm.sSCrm.CrmSouSuoKActivity
import com.erp.activity.StateKActivity
import com.erp.myAdapter.Listuserdataadapter
import com.erp.bean.CrmSouSuoBean
import com.erp.bean.Userdataonebean
import com.erp.myFragment.BaseKFragment
import com.erp.R
import com.erp.mclass.UnCeHandler
import com.erp.util.Myutil
import com.erp.module.crm.addCrm.AddCrmActivity
import com.erp.module.dingDan.prodyctionClient.ProdyctionClientActivity
import com.erp.permission.PermissionListener
import com.erp.util.PermissionUtils
import kotlinx.android.synthetic.main.fragment_one.*
import java.util.*

/**
 * Created by Administrator on 2016/11/2 0002.
 */
class FragmentCrm : BaseKFragment(), View.OnClickListener,CrmContract.CrmView, AdapterView.OnItemClickListener {
    
    private val f = "FragmentOne"
    lateinit var adapter: Listuserdataadapter
    var listdata: MutableList<Userdataonebean> = ArrayList()
    var updatedialog: AlertDialog? = null
    lateinit var P : ICrmP
    private var select: Int = 0
    companion object {
        var souSuoBean = CrmSouSuoBean()
    }
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_one
    }
//    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
//        Log.d("FFFF","setUserVisibleHint")
//        if (isVisibleToUser) {
//            isVisibl = true
//            UnCeHandler.fragment = fragmentname
//            initData()
//        } else {
//            isVisibl = false
//        }
//        super.setUserVisibleHint(isVisibleToUser)
//    }

    //加载数据方法
    override fun initData() {
        Myutil.Mylog("initData","++++++++++++++++")
//        if(souSuoBean.string != "") {
            P = ICrmP(context!!, this, listdata)
            adapter = Listuserdataadapter(listdata, this.activity)
            LvCrm.adapter = adapter
            souSuoBean = CrmSouSuoBean()
            RbProperty.setTextColor(ContextCompat.getColor(context!!, R.color.black))
            TvState.setTextColor(ContextCompat.getColor(context!!, R.color.black))
            RbProperty.text = "搜索"
            IvQingkong.visibility = View.GONE
            initClick()
            P.gainData("Crm")
            Pull.headerRefreshing()
//        }
    }
    //监听
    private fun initClick() {
        IvQingkong.setOnClickListener(this)
        RbProperty.setOnClickListener(this)
        TvState.setOnClickListener(this)
        LlXingJian.setOnClickListener(this)
        iv_scan.setOnClickListener(this)
        LvCrm.onItemClickListener = this
        Pull.setheader(true)
        Pull.setOnHeaderRefreshListener {
            select=0
            P.xiala()}
        Pull.setOnFooterRefreshListener {
            select=0
            P.shangla() }
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        when(parent){
            LvCrm -> {
//                if(listdata[position].crm_oid!="") {
                    val intent = Intent(activity, ClientKActivity::class.java)
                    intent.putExtra("id", listdata[position].crm_oid)
                    startActivity(intent)
//                }else{
//                    val intent = Intent(activity, ProdyctionClientActivity::class.java)
//                    intent.putExtra("id", listdata[position].oid)
//                    intent.putExtra("selsct", "0")
//                    intent.putExtra("all", "")
//                    startActivity(intent)
//                }
            }
        }
    }

    override fun onClick(v: View?) {
        when(v){
            IvQingkong -> {
                qingkong()
            }
            RbProperty -> {
                val intent = Intent(activity, CrmSouSuoKActivity::class.java)
                startActivityForResult(intent, 101)
                activity!!.overridePendingTransition(R.anim.start_activity1, R.anim.no_activity)
            }
            TvState -> {
                val intent = Intent(activity, StateKActivity::class.java)
                startActivityForResult(intent, 102)
                activity!!.overridePendingTransition(R.anim.start_activity1, R.anim.no_activity)
            }
            LlXingJian -> {
                xingjian()
            }
            iv_scan -> {
                PermissionUtils.issaomiao(activity!!, object : PermissionListener {
                    override fun onSucceed(requestCode: Int, grantPermissions: List<String>) {
                        val intent = Intent()
                        intent.setClass(activity!!, CaptureActivity::class.java)
                        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                        startActivityForResult(intent, 1)
                    }

                    override fun onFailed(requestCode: Int, deniedPermissions: List<String>) {
                        Myutil.MyTost("没有权限，无法扫描")
                    }
                })
            }
        }
    }

    fun qingkong(){
        Log.d("onSuccess",isFirstLoad.toString())
        if(isFirstLoad){
            return
        }
        RbProperty.setTextColor(ContextCompat.getColor(context!!,R.color.black))
        TvState.setTextColor(ContextCompat.getColor(context!!,R.color.black))
        souSuoBean = CrmSouSuoBean()
        Pull.headerRefreshing()
        RbProperty.text = "搜索"
        IvQingkong.visibility = View.GONE
    }
    //下拉刷新成功
    override fun xialaOk() {
        updatedialog?.dismiss()
        adapter.notifyDataSetChanged()
        Pull.onHeaderRefreshComplete()

        if (select == 3 || select == 4 || select == 5 && listdata.size != 0) {
            val intent = Intent(activity, ProdyctionClientActivity::class.java)
            intent.putExtra("id", listdata[0].oid)
            intent.putExtra("select", select)
            intent.putExtra("all", souSuoBean.all)
            startActivity(intent)
        }
    }
    //下拉刷新失败
    override fun xialaNo() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }
    //上拉加载成功
    override fun shanglaOk() {
        updatedialog?.dismiss()
        adapter.notifyDataSetChanged()
        Pull.onFooterRefreshComplete()
    }
    //上拉加载失败
    override fun shanglaNo() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        when (requestCode) {
            1 -> if (resultCode == Activity.RESULT_OK) {
                val bundle = data.extras
                //显示扫描到的内容
                var s: String = bundle?.get("result") as String
                Myutil.MyTost(s)
                RbProperty.text = "搜索"
                RbProperty.setTextColor(ContextCompat.getColor(context!!,R.color.green))
                TvState.setTextColor(ContextCompat.getColor(context!!,R.color.black))
                IvQingkong.visibility = View.GONE
                souSuoBean = CrmSouSuoBean()
                souSuoBean.all=s
                select=0
                updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                P.xiala()
                IvQingkong.visibility = View.VISIBLE
            }
            101 -> if (resultCode == Activity.RESULT_OK) {
                select=0
                updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                RbProperty.setTextColor(ContextCompat.getColor(context!!,R.color.green))
                TvState.setTextColor(ContextCompat.getColor(context!!,R.color.black))
                TvState.text= "统计"
                P.xiala()
                var sousuo = souSuoBean.string
                if(sousuo == ""){
                    RbProperty.text = "搜索"
                }else{
                    RbProperty.text= souSuoBean.string
                }
                IvQingkong.visibility = View.VISIBLE
            }
            102 -> {
                select=0
                updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
//                RbProperty.setTextColor(ContextCompat.getColor(context!!,R.color.black))
                TvState.setTextColor(ContextCompat.getColor(context!!,R.color.green))
                if(souSuoBean.statusname == ""){
                    TvState.text= "全部"
                }else{
                    TvState.text= souSuoBean.statusname

                }
                P.xiala()
            }
            50 -> {
                select=0
                if(data.getBooleanExtra("ok",false)){
                    souSuoBean = CrmSouSuoBean()
                    updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                    RbProperty.setTextColor(ContextCompat.getColor(context!!,R.color.green))
                    TvState.setTextColor(ContextCompat.getColor(context!!,R.color.black))
                    P.xiala()
                }
            }
        }
    }
    fun xingjian(){
        val i= Intent(activity, AddCrmActivity:: class.java)
        this.startActivityForResult(i,50)
    }
    fun setSelect(select: Int){
        this.select = select
    }
    fun update(s: String ) {
        souSuoBean = CrmSouSuoBean()
        souSuoBean.all=s
        updatedialog = Myutil.showupdatedialog(activity, "正在搜索", false)
        P.xiala()
    }

}

